<?php
/*
    NOR, simple feed agregator
    Copyright (C) 2011  Luis A. Arce González
    Copyright (C) 2011 	Víctor Ramírez 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
#20111111 Luis A. Arce First version

include_once("content.php");

class Style{
	
	private $id = NULL;
	private $className = NULL;
	private $newClass = NULL;

	private $applyToDoc     = 0x00;
	private $applyToContent = 0x00;
	private $applyToTable   = 0x00;
	private $applyToForm    = 0x00;

	/**
		@param divId is the id of the element in wich the specified class
		will be applied to the element itseld and it's childs relative to
		specified names in apply# variables. If no ID is specified it will
		be applyed to all elements
		@param class name to applyed to specifed elements. If no class is
		selected, any class declaration will be removed
	*/
	public function __construct($class, $id, $newClass){
		$this->className = $class;
		$this->id = $id;
		$this->newClass = $newClass;
	}

	public function addDocElms($docElms){
		$this->applyToDoc |= $docElms;
	
		return $this;
	}

	public function addTableElms($tableElms){
		$this->applyToDoc |= $tableElms;

		return $this;
	}

	public function addContentElms($tableElms){
		$this->applyToContent |= $tableElms;

		return $this;
	}

	public function addFormElms($formElms){
		$this->applyToForm |= $formElms;

		return $this;
	}

	public function apply($snippet){

		if($this->newClass!==NULL){
			if($this->id!==NULL){
				$snippet = $this->apply_by_attr($snippet, 'id', $this->id);
			}
			if($this->className!==NULL){
				$snippet = $this->apply_by_attr($snippet, 'class', $this->className);
			}
		}

		return $snippet;
	}

	private function apply_by_attr($snippet, $attName, $attValue){
	
		$xml = new SimpleXMLElement($snippet);	

		//We build an xpath expresion that retrieves all nodes 

		$curElms = array_merge(
			HTML_doc    ::getSelectedArrayString($this->applyToDoc), 
			HTML_content::getSelectedArrayString($this->applyToContent),
			HTML_form   ::getSelectedArrayString($this->applyToForm),
			HTML_table  ::getSelectedArrayString($this->applyToTable)
		);	

		if(count($curElms)!=0){		

			$xpathExpr = '//descendant-or-self::node()';
		
			if($attName!==NULL){
				$xpathExpr .= "[@$attName='$attValue']";
			}
			
			$xpathExpr .= '[' . $this->xpathElmTypeCondStr($curElms) . ']';
		
			$toModify = $xml->xpath($xpathExpr);


			foreach($toModify as $elem){
				$elem->addAttribute('class', $this->newClass);
			}
			//print($xpathExpr);	
			$snippet = str_replace("<?xml version=\"1.0\"?>\n", "", $snippet = $xml->asXML());
		}

		return $snippet;
			
	}

	private function xpathElmTypeCondStr($elmsArray){
		
		$str = '';

		if(count($elmsArray)!=0){
			$str.= ' local-name(.)=\'' . $elmsArray[0] . '\'';

			for($i=1; $i<count($elmsArray); $i++){
				$str.= ' or local-name(.)=\'' . $elmsArray[$i] . '\'';
			}
		}

		return $str;

	}
}



/*!#DEBUG
print "<pre>";

$style = new Style("main", "form");
$style->addDocElms(HTML_doc::BODY | HTML_doc::HEAD);
$style->addContentElms(HTML_content::STRONG | HTML_content::SMALL | HTML_content::IMG);

print($style->apply("<body id=\"form\"> <a id=\"form\"/>  <head id=\"form\"/></body>"));


print "</pre>";
#!*/


?>
